package com.sqsf.controller;


import com.sqsf.javabean.User;
import com.sqsf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 跳转到用户列表页
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<User> userList = userService.selectAllUsers();
        model.addAttribute("userList", userList);
        return "userList";
    }

    /**
     * 跳转到添加用户页
     */
    @GetMapping("/add")
    public String addPage() {
        return "addUser";
    }

    /**
     * 处理添加用户表单提交
     */
    @PostMapping("/add")
    public String addUser(User user) {
        userService.insertUser(user);
        return "redirect:/user/list";
    }

}